using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Taxonomy;

namespace MediaCentral.Services.SharePoint.Features.MediaCentral.Core
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("a0b69995-9efd-42a6-8394-1b25402c7fbe")]
    public class MediaCentralEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;


            TaxonomySession taxSession = new TaxonomySession(site);
            TermStore termStore = taxSession.TermStores["Managed Metadata Service"];


            // SERIOUSLY!?? Try/catch just to see if something exists??
            Group group = null;
            try
            {
                group = termStore.Groups["MediaCentral"];
            }
            catch (ArgumentException)
            {
                termStore.CreateGroup("MediaCentral");
                termStore.CommitAll();
                group = termStore.Groups["MediaCentral"];
            }


            TermSet tagsset = null;
            try
            {
                tagsset = group.TermSets["Videotags"];
            }
            catch (ArgumentException)
            {
                group.CreateTermSet("Videotags");
                termStore.CommitAll();
                tagsset = group.TermSets["Videotags"];
            }


            using (SPWeb web = site.OpenWeb())
            {
                // Add taxonomy field to LocalVideos List
                SPList localVidsList = web.Lists["LocalVideos"];

                // Trash the other content types from the local vids lib
                for (int i = localVidsList.ContentTypes.Count - 1; i > -1; i--)
                {
                    if (localVidsList.ContentTypes[i].Name != "Video")
                        localVidsList.ContentTypes[i].Delete();
                }

                if (!localVidsList.Fields.ContainsField("Tags"))
                {
                    TaxonomyField localVidsTagfield = localVidsList.Fields.CreateNewField("TaxonomyFieldType", "Tags") as TaxonomyField;
                    localVidsTagfield.SspId = termStore.Id;
                    localVidsTagfield.TermSetId = tagsset.Id;
                    localVidsTagfield.TargetTemplate = string.Empty;
                    localVidsTagfield.AllowMultipleValues = true;
                    localVidsTagfield.CreateValuesInEditForm = true;
                    localVidsTagfield.Open = true;
                    localVidsTagfield.AnchorId = Guid.Empty;
                    localVidsTagfield.Group = "MediaCentral Fields";
                    localVidsList.Fields.Add(localVidsTagfield);
                }

                localVidsList.Update();

                // Add Description, VideoID, Thumbnail and Tags columns to YouTubeVideos lib
                SPList ytVids = web.Lists["YouTubeVideos"];
                if (!ytVids.Fields.ContainsField("Description"))
                {
                    string newFieldName = ytVids.Fields.Add("Description", SPFieldType.Text, false);
                }

                if (!ytVids.Fields.ContainsField("VideoID"))
                {
                    string idField = ytVids.Fields.Add("VideoID", SPFieldType.Text, false);
                }

                if (!ytVids.Fields.ContainsField("Thumbnail"))
                {
                    string idField = ytVids.Fields.Add("Thumbnail", SPFieldType.Text, false);
                }

                if (!ytVids.Fields.ContainsField("Tags"))
                {
                    TaxonomyField yVidsTagsfield = ytVids.Fields.CreateNewField("TaxonomyFieldType", "Tags") as TaxonomyField;
                    yVidsTagsfield.SspId = termStore.Id;
                    yVidsTagsfield.TermSetId = tagsset.Id;
                    yVidsTagsfield.TargetTemplate = string.Empty;
                    yVidsTagsfield.AllowMultipleValues = true;
                    yVidsTagsfield.CreateValuesInEditForm = true;
                    yVidsTagsfield.Open = true;
                    yVidsTagsfield.AnchorId = Guid.Empty;
                    yVidsTagsfield.Group = "MediaCentral Fields";
                    ytVids.Fields.Add(yVidsTagsfield);
                }

                ytVids.Update();

                // SEt the masterpage to my html5, normal scrolling type
                web.MasterUrl = site.ServerRelativeUrl + "/_catalogs/masterpage/v4html5.master";
                web.Update();
            }

            // Set the homepage for the site to the interface page.
            site.RootWeb.RootFolder.WelcomePage = "MediaCentral/Home.aspx";

        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
